<template>
  <el-dialog
    :title="!dataForm.detailsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品图片" prop="commodityImg">
      <el-input v-model="dataForm.commodityImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="commodityMoney">
      <el-input v-model="dataForm.commodityMoney" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="commodityId">
      <el-input v-model="dataForm.commodityId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="送货状态0:未发货1:已发货" prop="stauts">
      <el-input v-model="dataForm.stauts" placeholder="送货状态0:未发货1:已发货"></el-input>
    </el-form-item>
    <el-form-item label="活动名称" prop="eventName">
      <el-input v-model="dataForm.eventName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="拼团记录id" prop="groupRecordId">
      <el-input v-model="dataForm.groupRecordId" placeholder="拼团记录id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除0:否1:是" prop="delStauts">
      <el-input v-model="dataForm.delStauts" placeholder="是否删除0:否1:是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          detailsId: 0,
          commodityImg: '',
          commodityMoney: '',
          commodityId: '',
          stauts: '',
          eventName: '',
          groupRecordId: '',
          createTime: '',
          updateTime: '',
          delStauts: ''
        },
        dataRule: {
          commodityImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          commodityMoney: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          commodityId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          stauts: [
            { required: true, message: '送货状态0:未发货1:已发货不能为空', trigger: 'blur' }
          ],
          eventName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          groupRecordId: [
            { required: true, message: '拼团记录id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStauts: [
            { required: true, message: '是否删除0:否1:是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.detailsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.detailsId) {
            this.$http({
              url: this.$http.adornUrl(`/leader/details/info/${this.dataForm.detailsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commodityImg = data.details.commodityImg
                this.dataForm.commodityMoney = data.details.commodityMoney
                this.dataForm.commodityId = data.details.commodityId
                this.dataForm.stauts = data.details.stauts
                this.dataForm.eventName = data.details.eventName
                this.dataForm.groupRecordId = data.details.groupRecordId
                this.dataForm.createTime = data.details.createTime
                this.dataForm.updateTime = data.details.updateTime
                this.dataForm.delStauts = data.details.delStauts
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/leader/details/${!this.dataForm.detailsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'detailsId': this.dataForm.detailsId || undefined,
                'commodityImg': this.dataForm.commodityImg,
                'commodityMoney': this.dataForm.commodityMoney,
                'commodityId': this.dataForm.commodityId,
                'stauts': this.dataForm.stauts,
                'eventName': this.dataForm.eventName,
                'groupRecordId': this.dataForm.groupRecordId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delStauts': this.dataForm.delStauts
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
